package org.example.listener;

import org.example.listener.log.DologObj;
import org.example.operator.OperatorField;
import org.example.undo.Undo;

import java.util.Objects;
import java.util.UUID;


/**
 *
 * 该类用于测试扩展使用
 * @author Silver
 */

public class UndoEventDologObjListener {
    private String name;

    /**
     * 以对象的方式记录undo操作
     */
    private Undo<DologObj> undo;

    public UndoEventDologObjListener(String name) {
        this.name = name;
    }

    public UndoEventDologObjListener(String name, Undo undo) {
        this.name = name;
        this.undo = undo;
    }

//    @Subscribe
//    @AllowConcurrentEvents
    public void listen(NumberEvent event){
        Object obj = event.getObj();
        if (Objects.isNull(obj)){
            //异常情况灵活处理
            return;
        }
        if (!(obj instanceof OperatorField)){
            //异常情况灵活处理
            return;
        }
        OperatorField operatorField = (OperatorField) obj;
        //填充undolog属性
        DologObj dologObj = new DologObj();
        dologObj.setResult(operatorField.getBeforeResult().getFinalResult());
        dologObj.setTxId(UUID.randomUUID().toString());
        undo.undoLog(dologObj);
//        System.out.println("监听者名字为 name = " + name + " 计算结果为：" + operatorField);
    }

}
